package com.douwong.student.task;

import android.os.AsyncTask;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.contant.NetConfig;
import com.douwong.student.interfaces.WorkResultNotify;
import com.douwong.student.model.ClassExam;
import com.douwong.student.model.PrepareBean;
import com.douwong.student.model.RequestAction;
import com.douwong.student.model.StatusCode;
import com.douwong.student.utils.NetUtils;
import com.douwong.student.utils.UIUtils;

import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * 课堂考试列表查询
 * @author leihuajie
 *
 */
public class ClassExamListTask extends AsyncTask<String, Integer, Integer>{

	private ArrayList<ClassExam> listData = null;
	private WorkResultNotify<ClassExam> workResult = null;
	public static int NO_DATA = 0;// 没有新的数据
	public static int REQUESTOK = 1;// 加载更多
	public static int NO_NETWOK = -1;// 没有网络
	public static int ERROR;
	private RequestAction action;
	private BaseApplication app;
	private String key;
	private boolean isSearch=false;

	public ClassExamListTask(RequestAction action, BaseApplication app, boolean isSearch) {
		this.app = app;
		this.action = action;
		this.isSearch=isSearch;

	}

	public void setWorkStautsListener(WorkResultNotify<ClassExam> workResult) {
		this.workResult = workResult;
	}
	@Override
	protected Integer doInBackground(String... params) {
		int resultCode = REQUESTOK;
		key = BaseApplication.sUserBean.getUserbean().getEuNumber() + "_classExam_"
				+ params[0];
		if (RequestAction.INIT == action) {
			if (app.readObject(key) != null) {
				listData = (ArrayList<ClassExam>) app.readObject(key);
				return resultCode;
			} else if (NetUtils.NetwrokIsUseful(UIUtils.getContext())) {
				resultCode = accessNetworkRequestData(params);
			} else {
				resultCode = NO_NETWOK;
			}
		} else if (NetUtils.NetwrokIsUseful(UIUtils.getContext())) {
			resultCode = accessNetworkRequestData(params);
		} else {
			resultCode = NO_NETWOK;
		}

		return resultCode;
	}
	private int accessNetworkRequestData(String... params) {
		String response = null;
		int resultCode = REQUESTOK;
		List<BasicNameValuePair> arrayParams = new ArrayList<BasicNameValuePair>();
		Log.e("classId", BaseApplication.sUserBean
				.getPersonalbean().getClassId()+"");
		Log.e("userId", BaseApplication.sUserBean.getUserbean().getId()+"");
		arrayParams.add(new BasicNameValuePair("userId", BaseApplication.sUserBean.getUserbean().getId()+""));
		arrayParams.add(new BasicNameValuePair("rows", "30"));
		arrayParams.add(new BasicNameValuePair("page", params[0]));
		if(params[1]!=null){
			arrayParams.add(new BasicNameValuePair("startTime", params[1]));
		}
		if(params[2]!=null){
			arrayParams.add(new BasicNameValuePair("endTime", params[2]));
		}
		if(params[3]!=null){
			arrayParams.add(new BasicNameValuePair("subjectId", params[3]));
		}
		try {
			response = NetUtils
					.httpGet(NetConfig.CLASS_EXAM_LIST, arrayParams);
			Log.e("response", response + "");
			if (response!=null){
				StatusCode<PrepareBean> sc = (StatusCode<PrepareBean>) JSON
						.parseObject(response,
								new TypeReference<StatusCode<PrepareBean>>() {
								});

				if (sc!=null&&sc.isFlag()) {
					Log.e("sc", sc.getData().getRows() + "");
					if ((sc.getData().getRows().equals("[]")||sc.getData().getRows() == null || sc.getData().getRows()
							.equals(""))&&action==RequestAction.REFRESH) {
						app.saveObject(listData, key);
						return NO_DATA;
					}
					if ((sc.getData().getRows().equals("[]")||sc.getData().getRows() == null || sc.getData().getRows()
							.equals(""))&&action==RequestAction.LOADMROE) {
						app.saveObject(listData, key);
						return NO_DATA;
					}
					listData = (ArrayList<ClassExam>) JSON.parseArray(sc
							.getData().getRows(), ClassExam.class);
					if(action==RequestAction.REFRESH){
						if(!isSearch){
							app.saveObject(listData, key);
						}
					}else if(action==RequestAction.INIT){
						app.saveObject(listData, key);
					}else {
						app.saveObject(listData, key);
					}
				}
			}else {
				return NO_DATA;
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultCode=ERROR;
		}
		return resultCode;
	}

	@Override
	protected void onPostExecute(Integer result) {
		super.onPostExecute(result);
		workResult.notify(result, listData);
	}

}
